package com.cw.jeeyt.service.impl.system;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemCountryBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemCountryDao;
import com.cw.jeeyt.dao.model.system.SystemCountryDo;
import com.cw.jeeyt.service.converter.system.SystemCountryConverter;
import com.cw.jeeyt.service.intf.system.SystemCountryService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 国家信息业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemCountryServiceImpl implements SystemCountryService {

    private static final Logger logger = LoggerFactory.getLogger(SystemCountryServiceImpl.class);
    @Autowired
    private SystemCountryDao systemCountryDao;//国家信息数据库操作接口

    /**
     * 新增SystemCountry
     *
     * @param systemCountryBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemCountry(SystemCountryBo systemCountryBo) throws Exception {

        if (systemCountryBo == null) {
            logger.error("国家信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemCountryDo systemCountry = SystemCountryConverter.bo2Do(systemCountryBo);
        systemCountry.setId(SequenceTool.nextId());
        systemCountry.setCreateBy(userVo.getId());
        systemCountry.setCreateDate(new Date());
        systemCountry.setUpdateBy(userVo.getId());
        systemCountry.setUpdateDate(new Date());
        systemCountryDao.insertSystemCountry(systemCountry);
    }

    /**
     * 更新SystemCountry信息
     *
     * @param systemCountryBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemCountry(SystemCountryBo systemCountryBo) throws Exception {

        if (systemCountryBo == null) {
            logger.error("国家信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemCountryDo systemCountry = SystemCountryConverter.bo2Do(systemCountryBo);
        systemCountry.setUpdateBy(userVo.getId());
        systemCountry.setUpdateDate(new Date());
        systemCountryDao.updateSystemCountry(systemCountry);
    }

    /**
     * 删除SystemCountry
     *
     * @param systemCountryBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemCountry(SystemCountryBo systemCountryBo) throws Exception {
        if (systemCountryBo == null) {
            logger.error("国家信息为空");
            return;
        }
        SystemCountryDo systemCountry = SystemCountryConverter.bo2Do(systemCountryBo);
        systemCountryDao.deleteSystemCountry(systemCountry);

    }

    /**
     * 分页查询SystemCountry
     *
     * @param systemCountryBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemCountryBo> queryByPage(SystemCountryBo systemCountryBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemCountryDo> pageInfo = systemCountryDao.queryByPage(SystemCountryConverter.bo2Do(systemCountryBo),
            startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemCountryConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询SystemCountry信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemCountryBo getSystemCountryById(String id) throws Exception {
        return SystemCountryConverter.do2Bo(systemCountryDao.getSystemCountryById(id));
    }
}
